﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManager : MonoBehaviour {

	[HideInInspector]
	public static AudioManager instance = null;

	private Dictionary<string, AudioClip> audios;
	private AudioSource[] audioSources;
	private AudioSource bgAudioSource;
	private AudioSource audioSourceEffect;

	void Awake () {
		instance = this;
		audios = new Dictionary<string, AudioClip> ();
		AudioClip[] audioArray = Resources.LoadAll<AudioClip> ("Audios");
		audioSources = GetComponents<AudioSource> ();
		bgAudioSource = audioSources [0];
		audioSourceEffect = audioSources [1];
		foreach (AudioClip a in audioArray) {
			audios.Add (a.name, a);
		}
		audioSources = GetComponents<AudioSource> ();
	}

	public void PlayBGaudio(string name){
		if (audios.ContainsKey (name)) {
			bgAudioSource.clip = audios[name];
			bgAudioSource.Play ();
		}
	}

	public void PlayAudioEffect(string name){
		if (audios.ContainsKey (name)) {
			if (name == "jump") {
				audioSourceEffect.volume = 0.7f;
			} else {
				audioSourceEffect.volume = 1.0f;
			}
			audioSourceEffect.clip = audios [name];
			audioSourceEffect.Play ();
		}
	}

	public void StopAllAudio(){
		bgAudioSource.Stop ();
		audioSourceEffect.Stop ();
	}

}
